Skip to content

c, cpp: add minimal Windows interop bindings - #26

Merged
xushiwei merged 1 commit into
goplus:mainfrom
cpunion:codex/windows-msvc-c-interop-20260822
Aug 28, 2026
Merged

c, cpp: add minimal Windows interop bindings#26
xushiwei merged 1 commit into
goplus:mainfrom
cpunion:codex/windows-msvc-c-interop-20260822

Conversation

@cpunion

@cpunion cpunion commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

Summary

Add only the native Windows C and C++ bindings currently required by the LLGo demo and compatibility-test suites:

  • UCRT standard streams, random numbers, file/process operations, time, and setjmp/longjmp
  • Winsock address resolution and socket operations, declared directly through the merged LLGo stdcall.* namespace
  • the release-mode MSVC std::string layout and wrappers
  • an LLVM 19 demangling bridge with a C ABI boundary

The package boundary remains platform-native. This PR does not emulate pthread, FLS, locks, semaphores, or Go closures on Windows. Existing POSIX pthread packages are simply excluded by Windows build constraints, and higher layers remain responsible for selecting a native thread implementation.

The non-Windows edits are limited to the build splits required to retain existing declarations and two ABI corrections exercised by the shared demos: ctime returns char *, and freeaddrinfo returns void.

Windows setjmp/longjmp are LLGo intrinsics so lowering occurs in the caller rather than in a wrapper frame that has already returned. The compiler support is already merged through xgo-dev/llgo#2425. Direct Winsock declarations use the stdcall support merged through xgo-dev/llgo#2433; this PR CI pins that merged xgo-dev/llgo commit and has no R9 dependency.

Validation

  • go test ./... on macOS ARM64
  • macOS LLGo execution of c, c/math/rand, c/net, c/os, c/setjmp, c/time, cpp/llvm, and cpp/std
  • PR CI passes Go build plus LLGo execution on Ubuntu, macOS, and Windows
  • LLGo R9 cross-platform ssa golden compilation passes with this commit used only by _demo/c, cl/_testlibc, cl/_testpy, and cl/_testrt

This work is part of the LLGo Windows support proposal.

@cpunion

cpunion commented Aug 27, 2026

Copy link
Copy Markdown
Contributor Author

Validation update:

  • Added LLGo-native unit tests for the raw Once.Do callback, a captured DoFunc closure, concurrent callers with distinct contexts, and nested DoFunc calls.
  • llgo test -v ./c/pthread/sync passes locally on macOS ARM64 and Windows 11 ARM64.
  • The new Linux LLGo CI job passed all four tests using released LLGo v1.0.0 and LLVM 19: https://github.com/goplus/lib/actions/runs/33037946987/job/98404795022
  • The ordinary Go build job remains green.

@cpunion

cpunion commented Aug 27, 2026

Copy link
Copy Markdown
Contributor Author

LLGo execution coverage is now automated on both host platforms:

Both jobs build LLGo v1.0.0 with LLVM 19 and run all four c/pthread/sync tests.

Windows 11 ARM64 has also passed the same tests locally with the staged R8 integration. I additionally built and tested the latest merged LLGo Windows commit (71754b459, R5): LLGo itself builds, but llgo test still fails while compiling standard-library internal/fuzz due invalid LLVM IR. Windows CI should therefore be added after the remaining R6 testing support is merged, rather than pinning this upstream PR to an unmerged fork commit.

@cpunion cpunion changed the title c: support Windows MSVC interop c, cpp: support Windows MSVC interop Aug 27, 2026
@cpunion cpunion closed this Aug 28, 2026
@cpunion cpunion reopened this Aug 28, 2026
@cpunion
cpunion force-pushed the codex/windows-msvc-c-interop-20260822 branch from d1bee6f to cda1bc5 Compare August 28, 2026 14:25
@cpunion
cpunion marked this pull request as ready for review August 28, 2026 14:31
@cpunion cpunion changed the title c, cpp: support Windows MSVC interop c, cpp: add minimal Windows interop bindings Aug 28, 2026

@fennoai fennoai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review: minimal Windows interop bindings

This is a clean, well-scoped PR. The platform split into _default.go (!windows) and _windows.go files is consistent and the build constraints are mutually exclusive and complete — no GOOS is left without a definition, and no symbol is doubly defined. Struct layouts are notably well-verified: WSAData (386 vs 64, sizes 400/408), the Winsock Sockaddr*/Hostent/AddrInfo structs, _stat64 (size 56), and the release-mode MSVC std::string layout are all backed by explicit unsafe.Sizeof/Offsetof regression tests and a C++ static_assert. The C++ ABI is correctly kept behind a C shim (demangle_windows.cpp + extern "C"), and the returned char* ownership contract (caller frees) is matched by c.Free in the tests. No performance, memory-safety, or documentation-contradiction issues were found.

A few low-severity, non-blocking notes:

CI checks out a non-goplus org (xgo-dev/llgo).github/workflows/go.yml
The LLGo checkout points at xgo-dev/llgo, pinned to commit 6fc2ef150f95d8b9331433f325fcb1b6ee5ad4e4. The full-SHA pin is the right hardening (immutable, cannot be silently re-pointed), and this runs with a restricted token on pull_request with no secrets exposed. Worth confirming xgo-dev is the intended/trusted source and that the referenced stdcall support (xgo-dev/llgo#2433) is expected to land in the canonical repo before merge.

CanOnName field namec/net/addrinfo_windows.go:17
Maps C ai_canonname. The name mis-splits the word ("can on name"), but it is intentional consistency with the pre-existing non-Windows declaration in c/net/net.go:212, not a new typo. Left as-is it keeps both platforms aligned; only worth touching if the awkward name is corrected in both files together (a public-API rename).

Header / build-tag style inconsistency (cosmetic)
New files mix the full Apache block header with the one-line Copyright ... Licensed under the Apache License 2.0. form, and a couple of files (c/stdio_windows.go, c/ctypes_windows.go) carry both //go:build and the legacy // +build lines while most use only //go:build. Worth normalizing to the repo standard.

Nothing here blocks the PR.

@zhouguangyuan0718

Copy link
Copy Markdown

Replacement PR #27 targets goplus/lib directly and preserves this PR current squashed head as its first commit, with the LLVM 21 update as a separate second commit.

@xushiwei
xushiwei merged commit e1337dd into goplus:main Aug 28, 2026
4 checks passed
xushiwei added a commit to xgo-dev/llgo that referenced this pull request Aug 29, 2026
…in-20260827

windows: support independent MSVC and MinGW profiles (R9, depends on goplus/lib#26)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants